Skip to main content

Functions

Functions of a Single Variable

A function describes the relationship between variables.

Examples:

f(x)=x2f(x) = x^2

y=2+3x4y = 2+3\cdot x^4

Fig. 4

Details

Functions are commonly used in statistical applications, to describe relationships.

Definition

A function describes the relationship between variables. A variable yy is described as a function of a variable xx by completely specifying how yy can be computed for any given value of xx.

An example could be the relationship between a dose level and the response to the dose.

The relationship is commonly expressed by writing either f(x)=x2f(x) = x^{2} or y=x2y = x^2.

Usually names are given to functions, i.e. to the relationship itself. For example, ff might be the function and f(x)f(x) could be its value for a given number xx. Typically f(x)f(x) is a number but ff is the function, but the sloppy phrase "the function f(x)=2x+4f(x)=2x+4" is also common.

Examples

Example

f(x)=x2f(x) = x^2 or y=x2y = x^2 specifies that the computed value of yy should always be x2x^2, for any given value of xx.

Functions in R

A function can be defined in R using the function command:

Fig. 5

Ranges and Plots in R

Functions in R can commonly accept a range of values and will return a corresponding vector with the outcome.

Examples

Example
f <- function(x) {return(x*12)}
x <- seq (-5,5,0,1)
y <- f(x)
plot {(x,y) type= 'l'}

Plotting Functions

In statistics, the function of interest is commonly called the response function. If we write y=f(x)y=f(x), the outcome yy is usually called the response variable and xx is the explanatory variable. Function values are plotted on vertical axis while xx values are plotted on horizontal axis. This plots yy against xx.

Examples

Example

The following R commands can be used to generate a plot for function y=2+3xy= 2+3x:

x <- seq(0:10)
g <- function(x) {
+ yhat <- 2+3*x
+ return(yhat)
+ }

x <- seq(0,10,0.1)
y <- g(x)
plot(x,y,type="l", xlab="x",ylab="y")

Functions of Several Variables

Examples

Example

z=2x+3y+4v=t2+3xw=t2+3bx\begin{aligned} z &= 2x+3y+4\\ v &= t^2+3x\\ w &= t^2+3b \cdot x\end{aligned}